package com.bdqn.controller;


import com.bdqn.entity.Dish;
import com.bdqn.entity.Setmeal;
import com.bdqn.entity.SetmealDish;
import com.bdqn.param.requset.SetMealDto;
import com.bdqn.param.response.DishDto;
import com.bdqn.param.response.Page;
import com.bdqn.service.DishService;
import com.bdqn.service.SetmealDishService;
import com.bdqn.service.SetmealService;
import com.bdqn.util.RespBean;
import com.bdqn.util.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-06-11
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public RespBean page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, String name) {
        Page<Setmeal> setmealPage = setmealService.pageInfo(page, pageSize, name);
        return setmealPage != null ? RespBean.ok("获取套餐列表成功", setmealPage) : RespBean.error("获取套餐列表失败");
    }


    /**
     * 添加一条套餐信息   添加一条套餐信息的同时 添加多条菜品id在 菜品和套餐关系表中并与套餐id进行绑定
     */
    @PostMapping
    public RespBean addSetMeal(@RequestBody SetMealDto setMealDto) {
        try {
            log.info("接收到的数据为: {}", setMealDto);
            boolean save = setmealService.save(setMealDto);
            if (!save) {
                return new RespBean(ResultEnum.FAIL);
            }
            //获取到每个关系对象
            for (SetmealDish setmealDish : setMealDto.getSetmealDishes()) {
                setmealDish.setSetmealId(setMealDto.getId());
                setmealDishService.save(setmealDish);
            }
            return RespBean.ok("添加成功", 1);
        } catch (Exception e) {
            return RespBean.error("添加失败");
        }
    }


    /**
     * 修改订单的状态
     */
    @PostMapping("/status/{code}")
    public RespBean updateStatus(@PathVariable Integer code, Long[] ids) {
        try {
            Setmeal setmeal = new Setmeal();
            for (Long id : ids) {
                setmeal.setStatus(code);
                setmeal.setId(id);
                setmealService.updateById(setmeal);
            }
            return RespBean.ok("更改状态成功", 1);
        } catch (Exception e) {
            return RespBean.error("更改状态失败");
        }
    }


    @DeleteMapping
    public RespBean delete(Long[] ids) {
        System.out.println("获取的传递的参数为" + Arrays.toString(ids));
        try {
            for (Long id : ids) {
                //这里我们也可以直接通过传入集合进行删除
                setmealService.removeById(id);
            }
            return RespBean.ok("删除套餐成功", 1);
        } catch (Exception e) {
            return RespBean.error("删除套餐失败");
        }
    }

    /**
     * 通过前台传来的category的id 进行展示
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public RespBean dish_id(@PathVariable Long id) {
        DishDto dishDto = dishService.dishDto(id);
        return  dishDto!=null?RespBean.ok("获取菜品信息和口味成功",dishDto):RespBean.error("获取菜品信息和口味失败");
    }

    /**
     * 通过套餐的分类id查询该套餐id下启动状态的套餐列表
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public RespBean list(Long categoryId,Integer status){
     log.info("接收前台传来的参数为: {},{}",categoryId,status);
        List<Setmeal> setMealList=setmealService.selectByCategoryId(categoryId,status);
        return setMealList!=null?RespBean.ok("获取套餐分类下的套餐列表成功",setMealList):RespBean.error("获取套餐分类下的套餐列表失败");
    }
}
